<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 租房管理
 */
class RentController extends BaseController
{
	public function getCode ($length = 3, $mode = 0){
	    switch ($mode) {
	    case '1':
	    $str = '1234567890';
	    break;
	    case '2':
	    $str = 'abcdefghijklmnopqrstuvwxyz';
	    break;
	    case '3':
	    $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    break;
	    default:
	    $str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    break;
	    }

	    $result = '';
	    $l = strlen($str)-1;
	    $num=0;

	    for($i = 0;$i < $length;$i ++){
	        $num = rand(0, $l);
	        $a=$str[$num];
	        $result =$result.$a;
	    }
		return $result;
	}

    public function index($key="")
    {
        if($key === ""){
            $model =D('Rent');  
        }else{
            $where['title'] = array('like',"%$key%");
            $where['area'] = array('like',"%$key%");
            $where['tube'] = array('like',"%$key%");
            $where['_logic'] = 'or';
            $model =D('Rent')->where($where); 
        }
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $pages = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $this->assign('model', $pages);
        $this->assign('page',$show);
        $this->display();     
    }


    /**
     * 更新单页信息
     */
    public function update($id)
    {   header("Content-Type:text/html; charset=utf-8");
	    $id = intval($id);	
		
	    $fenrenta = M('fenrent')->field('fanghao')->where('rel_id="'.$id.'" and fanghao="房间A"')->find();	
		if($fenrenta){
	    $ida = M('fenrent')->field('id')->where('rel_id="'.$id.'" and fanghao="房间A"')->find();	
		$this->assign('fenrenta',1);
		$this->assign('ida',$ida['id']);
		}
		
	    $fenrentb = M('fenrent')->field('fanghao')->where('rel_id="'.$id.'" and fanghao="房间B"')->find();	
		if($fenrentb){
	    $idb = M('fenrent')->field('id')->where('rel_id="'.$id.'" and fanghao="房间B"')->find();	
		$this->assign('fenrentb',1);
		$this->assign('idb',$idb['id']);
		}	
		
	    $fenrentc = M('fenrent')->field('fanghao')->where('rel_id="'.$id.'" and fanghao="房间C"')->find();	
		if($fenrentc){
	    $idc = M('fenrent')->field('id')->where('rel_id="'.$id.'" and fanghao="房间C"')->find();	
		$this->assign('fenrentc',1);
		$this->assign('idc',$idc['id']);
		}		
		
	    $fenrentd = M('fenrent')->field('fanghao')->where('rel_id="'.$id.'" and fanghao="房间D"')->find();	
		if($fenrentd){
	    $idd = M('fenrent')->field('id')->where('rel_id="'.$id.'" and fanghao="房间D"')->find();	
		$this->assign('fenrentd',1);
		$this->assign('idd',$idd['id']);
		}
		
		
		
		
        if (!IS_POST) {
            $rent = M('rent')->where("rent.id=%d",$id)->find();
            $peizhi=$rent['peizhi'];
			$peizhixz = explode('|',$peizhi); 
            /*房源配置*/
			if (in_array("床",$peizhixz)) 
			{ 
			 $this->assign('peizhi1',1);
			} 
			if (in_array("空调",$peizhixz)) 
			{ 
			 $this->assign('peizhi2',1);
			} 
			if (in_array("热水器",$peizhixz)) 
			{ 
			 $this->assign('peizhi3',1);
			} 
			if (in_array("冰箱",$peizhixz)) 
			{ 
			 $this->assign('peizhi4',1);
			}
			if (in_array("电视",$peizhixz)) 
			{ 
			 $this->assign('peizhi5',1);
			}
			if (in_array("家具",$peizhixz)) 
			{ 
			 $this->assign('peizhi6',1);
			}
			if (in_array("独立卫生间",$peizhixz)) 
			{ 
			 $this->assign('peizhi7',1);
			}		

            /*房源特色*/
			$tese=$rent['tese'];
			$tesexz=explode('|',$tese);
			if (in_array("洗衣机",$tesexz)) 
			{ 
			 $this->assign('tese1',1);
			} 
			if (in_array("宽带",$tesexz)) 
			{ 
			 $this->assign('tese2',1);
			} 
			if (in_array("阳台",$tesexz)) 
			{ 
			 $this->assign('tese3',1);
			}
			if (in_array("厨房",$tesexz)) 
			{ 
			 $this->assign('tese4',1);
			}  
			if (in_array("游泳池",$tesexz)) 
			{ 
			 $this->assign('tese5',1);
			} 
			if (in_array("停车位",$tesexz)) 
			{ 
			 $this->assign('tese6',1);
			} 	
			if (in_array("电梯",$tesexz)) 
			{ 
			 $this->assign('tese7',1);
			} 											
            $this->assign('rent',$rent);
            $this->display();
        }
        if (IS_POST) {
            $model = D("Rent");
           //----------------------------------------------------------
		   if(!empty($_FILES)){  //判断是否有文件上传
		   $config = array(
		   'rootPath'      =>  './Public/', //保存根路径
		   'savePath'      =>  'rent/', //保存路径
		   );
			$upload = new \Think\Upload($config); 
            $cg = $upload ->upload(array($_FILES['pic']));//调用upload函数并将参数$_FILES数组传进去
			if($cg){
			//循环输出拼装好的图片路径给$bigimg
			foreach($cg as $vo)
			{
			$bigimg.=$vo['savepath'].$vo['savename']."|";
			$_POST['imgurl'] = $bigimg;
			}
			}
			
           $cglist = $upload -> upload(array($_FILES['piclist']));
			if($cglist){
			foreach($cglist as $vo1)
			{
			$bigimg1.=$vo1['savepath'].$vo1['savename']."|";
			$_POST['imgurl_list'] = $bigimg1;
			}
			}
			
           $cgct = $upload -> upload(array($_FILES['picct']));
			if($cgct){
			foreach($cgct as $vo2)
			{
			$bigimg2.=$vo2['savepath'].$vo2['savename']."|";
			$_POST['imgurl_ct'] = $bigimg2;
			}
			}	
			
			}	
			
			foreach($_POST['peizhi'] as $vo)
			{
			$peizhi.=$vo."|";
			$_POST['peizhi'] =$peizhi;
			}
			
			foreach($_POST['tese'] as $vo)
			{
			$tese.=$vo."|";
			$_POST['tese'] =$tese;
			}
			
            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {
			       $model->id = $id;
                   $data= M('rent')->field("imgurl,imgurl_list,imgurl_ct")->where("rent.id=%d",$id)->find();
			        $model->imgurl=$data['imgurl'].$bigimg;
			        $model->imgurl_list=$data['imgurl_list'].$bigimg1;
			        $model->imgurl_ct=$data['imgurl_ct'].$bigimg2;
			
                if ($model->save()) {
                    $this->success("更新成功", U('Rent/index'));
                } else {
                    $this->error("更新失败");
                } 
            }
        }
    }
    /**
     * 删除单页
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
    		$id = intval($id);
        $model = M('rent');
        $model->flag = 0; 
        $result = $model->where("id=%d",array($id))->save();
        if($result){
            $this->success("下架成功", U('Rent/index'));
        }else{
            $this->error("下架失败");
        }
    }
	
         //删除数组指定值	
         public function delarr_1($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('rent')->field("imgurl")->where("id=%d",$id)->find();
             $datav=$data['imgurl'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("rent");
             $data['imgurl'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo '数据更新成功！';
             }else{
             echo '数据更新失败！';
             }
             }
	
         //删除数组指定值	
         public function delarr_2($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('rent')->field("imgurl_list")->where("id=%d",$id)->find();
             $datav=$data['imgurl_list'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("rent");
             $data['imgurl_list'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo '数据更新成功！';
             }else{
             echo '数据更新失败！';
             }
             }

 
         //删除数组指定值	
         public function delarr_3($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('rent')->field("imgurl_ct")->where("id=%d",$id)->find();
             $datav=$data['imgurl_ct'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("rent");
             $data['imgurl_ct'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo '数据更新成功！';
             }else{
             echo '数据更新失败！';
             }
             }
	
    public function add(){
    	if(!IS_POST){
    		$bianhao = "HFD".date('ymd').R('Rent/getCode');
    		$this->assign('bianhao',$bianhao);
			$this->display();
    	}else{
    		$data['title']			=	 	$_POST['title'];
    		$data['bianhao']		=	 	$_POST['bianhao'];
    		$data['area']			=	 	$_POST['area'];
    		$data['dizhi']			=	 	$_POST['dizhi'];
    		$data['xiaoqu']			=		$_POST['title'];
    		$data['tube']			=	 	$_POST['tube'];
    		$data['price_area'] 	=	 	$_POST['price_area'];
    		$data['mianji']			= 		$_POST['mianji'];
    		$data['mianji_area']	= 		$_POST['mianji_area'];
    		$data['huxing']			= 		$_POST['huxing'];
    		$data['peizhi']			= 		$_POST['peizhi'];
    		$data['tese']			= 		$_POST['tese'];
    		$data['zulinfs']		=	 	$_POST['zulinfs'];
    		$data['chaoxiang']		= 		$_POST['chaoxiang'];
    		$data['zhuangxiu']		=	 	$_POST['zhuangxiu'];
    		$data['floor']			= 		$_POST['floor'];
    		$data['recommend']		= 		$_POST['recommend'];
    		$data['addtime']		= 		time();
    		M("rent")->data($data)->add();
    		$rentid=M()->query("SELECT max(id) FROM rent");
    		$model=D('Rent');
    		if (!empty($_FILES)) {
    			$config = array(
				   'rootPath'      =>  './Public/', //保存根路径
				   'savePath'      =>  'rent/'.$_POST['title'].'/', //保存路径
				   );
    			$upload = new \Think\Upload($config); 
    			$upinfo1 = $upload -> uploadOne($_FILES['piclist']);
    			if(!$upinfo1){
    				$this->error($upload1->getError());
    			}else{
    				print_r($_POST['picct']);
    				// foreach ($upinfo1 as $upsql1) {
    				// 	M()->execute("INSERT into imgurl_list (rentid,listimgurl) values ('".$rentid['id']."','".$upsql1['savename']."')");
    				// }
    			}
    			$upload2 = $upload -> uploadOne($_FILES['picct']);
    			if(!upinfo2){
    				$this->error($upload2->getError());
    			}else{
    				foreach ($upinfo2 as $upsql2) {
    					M()->execute("INSERT into imgurl_ct (rentid,ctimgurl) values ('".$rentid['id']."','".$upsql2['savename']."')");
    				}
    			}
    			$upload3 = $upload -> uploadOne($_FILES['picprice']);
    			if(!upinfo3){
    				$this->error($upload3->getError());
    			}else{
    				foreach ($upinfo3 as $upsql3) {
    					M()->execute("INSERT into price_img (rentid,imgurl) values ('".$rentid['id']."','".$upsql3['savename']."')");
    				}
    			}
    		}
    	}
    }

	
}
